import threading
import time
from typing import Callable, Any, Iterable, Mapping


class MyThread(threading.Thread):

    def __init__(self, threadID, name, delay):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.delay = delay

    def run(self) -> None:
        print(f'线程{self.name}执行任务.....')
        sum1 = 0
        for i in range(1, 10):
            sum1 += i
            print(f'线程{self.name} sum1 = {sum1}')
            time.sleep(self.delay)

        print(f'sum = {sum1} 线程{self.name} {time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}完成任务.....')



if __name__ == '__main__':
    try:
        t1 = MyThread(1, "线程1", 1)
        t2 = MyThread(2, "线程2", 1)
        t1.start()
        t2.start()
        # join 堵塞主线程，让线程1和线程2执行完
        t1.join()
        t2.join()
        print("退出主线程")
    except:
        print("Error: 无法启动线程")

